package com.siwei.mes.controller.order;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.order.Order;
import com.siwei.mes.service.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 工单(Order)表控制层
 *
 * @author makejava
 * @since 2023-12-05 09:31:31
 */
@Api(value = "工单")
@RestController
@RequestMapping("order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @ApiOperation(value = "新增工单")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) Order entity) {
        return Json.succ().data(ApiResult.addColumn("id", orderService.create(entity)));
    }

    @ApiOperation(value = "修改工单")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) Order entity) {
        orderService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除工单")
    @PostMapping("/delete")
    public Json delete(@RequestBody Order entity) {
        orderService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取工单")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<Order, Order> query) {
        return Json.succ().data(orderService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取工单")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(orderService.loadById(id));
    }

    @ApiOperation(value = "获取所有工单")
    @PostMapping("getAll")
    public Json getAll(@RequestBody Order entity) {
        return Json.succ().data(ApiResult.addList(orderService.getAll(entity)));
    }

    /**
     * 转派
     *
     * @return
     */
    @ApiOperation(value = "转派")
    @PostMapping("transfer")
    public Json transfer(@RequestBody Order entity) {
        assertTrue(entity.getId() == null, "工单id为空");
        assertTrue(entity.getUserId() == null, "客服员id为空");
        assertTrue(StringUtils.isBlank(entity.getUserName()), "客服员名称为空");
        orderService.transfer(entity);
        return Json.succ().data(new ApiResult());
    }

    /**
     * 完成
     *
     * @return
     */
    @ApiOperation(value = "完成")
    @PostMapping("finish")
    public Json finish(@RequestBody Order entity) {
        assertTrue(entity.getId() == null, "工单id为空");
        assertTrue(entity.getGenQuick() == null, "是否需要生成快检为空");
        orderService.finish(entity);
        return Json.succ().data(new ApiResult());
    }

    /**
     * 接收
     *
     * @return
     */
    @ApiOperation(value = "接收")
    @PostMapping("accept")
    public Json accept(@RequestBody Order entity) {
        assertTrue(entity.getId() == null, "工单id为空");
        orderService.accept(entity);
        return Json.succ().data(new ApiResult());
    }

    /**
     * 拒绝
     *
     * @return
     */
    @ApiOperation(value = "拒绝")
    @PostMapping("refuse")
    public Json refuse(@RequestBody Order entity) {
        assertTrue(entity.getId() == null, "工单id为空");
        orderService.refuse(entity);
        return Json.succ().data(new ApiResult());
    }

    /**
     * 作废
     *
     * @return
     */
    @ApiOperation(value = "作废")
    @PostMapping("cancel")
    public Json cancel(@RequestBody Order entity) {
        assertTrue(entity.getId() == null, "工单id为空");
        orderService.cancel(entity);
        return Json.succ().data(new ApiResult());
    }
}
